package com.yjx.gulimall.product.service.impl;

import com.yjx.gulimall.common.constant.ObjectConstant;
import com.yjx.gulimall.common.vo.product.Attr;
import com.yjx.gulimall.common.vo.product.Images;
import com.yjx.gulimall.common.vo.product.Skus;
import com.yjx.gulimall.product.agent.CouponAgentService;
import com.yjx.gulimall.product.entity.SkuImagesEntity;
import com.yjx.gulimall.product.entity.SkuSaleAttrValueEntity;
import com.yjx.gulimall.product.entity.SpuInfoEntity;
import com.yjx.gulimall.product.service.SkuImagesService;
import com.yjx.gulimall.product.service.SkuSaleAttrValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yjx.gulimall.common.utils.PageUtils;
import com.yjx.gulimall.common.utils.Query;

import com.yjx.gulimall.product.dao.SkuInfoDao;
import com.yjx.gulimall.product.entity.SkuInfoEntity;
import com.yjx.gulimall.product.service.SkuInfoService;
import org.springframework.util.CollectionUtils;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Autowired
    private SkuImagesService skuImagesService;

    @Autowired
    private SkuSaleAttrValueService skuSaleAttrValueService;

    @Autowired
    private CouponAgentService couponAgentService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }


    @Override
    public void saveSkuInfo(SpuInfoEntity spuInfoEntity, List<Skus> skus) {
        //   6.1)sku的基本信息：pms_sku_info
        //   6.2)sku的图片信息：pms_sku_images
        //   6.3)sku的销售属性值：pms_sku_sale_attr_value
        //   6.4)sku的打折（买几件打几折）、满减信息（满多少减多少）、会员价格：
        //   sms_sku_ladder\sms_sku_full_reduction\sms_member_price
        if (!CollectionUtils.isEmpty(skus)) {
            skus.forEach(skus1 -> {

                List<Images> skuImages = skus1.getImages();
                //获取默认图片地址
                Images defaultImage = skuImages.stream().filter(
                        images -> ObjectConstant.BooleanIntEnum.YES.getCode().equals(images.getDefaultImg()))
                        .findAny().orElse(new Images());

                SkuInfoEntity skuInfoEntity = new SkuInfoEntity();
                skuInfoEntity.setSkuName(skus1.getSkuName());
                skuInfoEntity.setSkuDefaultImg(defaultImage.getImgUrl());
                skuInfoEntity.setSkuTitle(skus1.getSkuTitle());
                skuInfoEntity.setSkuSubtitle(skus1.getSkuSubtitle());
                skuInfoEntity.setSpuId(spuInfoEntity.getId());
                skuInfoEntity.setCatalogId(spuInfoEntity.getCatalogId());
                skuInfoEntity.setBrandId(spuInfoEntity.getBrandId());
                //skuInfoEntity.setSkuDesc(); sku描述
                skuInfoEntity.setPrice(skus1.getPrice());
                this.save(skuInfoEntity);
                Long skuId = skuInfoEntity.getSkuId();

                //保存sku 图片信息
                boolean imageFlag = skuImagesService.saveBatchInfo(skuId,skuImages);

                //保存销售属性
                boolean attrFlag = skuSaleAttrValueService.saveBatchInfo(skuId,skus1.getAttr());

                //sku的打折（买几件打几折）、满减信息（满多少减多少）、会员价格
                couponAgentService.saveSkuReduction(skuId,skus1);

            });




        }
    }

}